package com.es.main;

import java.sql.ResultSet;
import java.util.ArrayList;

import com.es.util.db.Dao;

public class LoginBiz {

	@SuppressWarnings("unchecked")
	public LoginUserBeen login(ArrayList param)throws Exception {

		LoginUserBeen loginUser = null;
		Dao dao = new Dao();
		String sql = "select top 1 emp.emp_id login_id,emp.emp_code,emp_login_id login_name,emp_area_value,emp_area_code,emp_province_value,emp_province_code,emp_city_value,emp_city_code,emp_shiyebu,emp_login_pwd login_pwd,emp_name login_user_name,emp_role login_role"
				+" from hrm_emp_info emp where emp.del_flag='N' and  ltrim(rtrim(emp.emp_login_yn)) ='Y' and emp_login_id = ? and emp_login_pwd = ?";
		try {
			ResultSet resultSet = dao.Select(sql,param);
			if(resultSet.next()){
				loginUser = new LoginUserBeen();
				loginUser.setLoginId(resultSet.getString("login_id"));
				loginUser.setLoginName(resultSet.getString("login_name"));
				loginUser.setLoginUserName(resultSet.getString("login_user_name"));				
				loginUser.setLoginRole(resultSet.getString("login_role"));
				loginUser.setCity(resultSet.getString("emp_city_value"));
				loginUser.setEara(resultSet.getString("emp_area_value"));
				loginUser.setProvince(resultSet.getString("emp_province_value"));
				loginUser.setCity_code(resultSet.getString("emp_city_code"));
				loginUser.setEara_code(resultSet.getString("emp_area_code"));
				loginUser.setProvince_code(resultSet.getString("emp_province_code"));
				loginUser.setShiyebu(resultSet.getString("emp_shiyebu"));
				loginUser.setEmp_code(resultSet.getString("emp_code"));
				//loginUser.setLoginRemark(resultSet.getString("login_remark"));			
			}
		} finally{
			dao.close();
		}
		if(loginUser!=null)
			loginUser.setMenuList(getEmpMenu(loginUser.getLoginRole()));
		return loginUser;
	}
	
	public ArrayList<HrmMenu> getEmpMenu(String role)throws Exception
  {
		Dao dao = new Dao();
		HrmMenu menu = null;
	    ArrayList<HrmMenu> menuList = new ArrayList<HrmMenu>();
	    ArrayList<String> queryParm = new ArrayList<String>();
	    queryParm.add(role); 
	    
	    String sql = "select m.menu_id,m.menu_name,m.parent_id,m.menu_url,m.menu_desc from hrm_menu_role r " +
	    		"inner join hrm_menu m on r.menu_id=m.menu_id where r.role_id=? and m.use_flag='Y' order by r.menu_id";
	    try {
	    	ResultSet rs = dao.Select(sql,queryParm);
	      while (rs.next())
	      {
	        menu = new HrmMenu();
	        menu.setMenuId(rs.getString("menu_id"));
	        menu.setMenuName(rs.getString("menu_name"));
	        menu.setParentId(rs.getString("parent_id"));
	        menu.setMenuUrl(rs.getString("menu_url"));
	        menu.setMenuDesc(rs.getString("menu_desc"));
	        menuList.add(menu);
	      }
	    }
	    finally {
	    	dao.close();
	    }
	   /* if(menuList.size()==0)
	    	return null;*/
	    return menuList;
  }

}
